import { createSlice } from '@reduxjs/toolkit'
const tabSlice = createSlice({
    name: 'tab',
    initialState: {
        isCollapse: false,
        tabList:[
            {
                path:'/home',
                name:'home',
                label:'首页'
            }
        ],
        currentMenu:{},
        HighlightKey:['/home'],//侧边栏高亮
        crumbList:[
            {
                path:'/home',
                name:'home',
                title:'首页'
            }
        ]
    },
    reducers: {
        collapseMenu: state => {
            state.isCollapse = !state.isCollapse

        },
        selectMenuList:(state,action)=>{
            if(action.payload.name!=='home'){
                state.currentMenu=action.payload
                const res=state.tabList.findIndex((item)=>{return item.name===action.payload.name})
                if(res===-1){
                    state.tabList.push(action.payload)
                    state.currentMenu=action.payload
                }
            }
        },
        closeTab:(state,action)=>{
            let res= state.tabList.findIndex((item,index)=>{return item.name===action.payload.name})
            state.tabList.splice(res,1)
        },
        currentMenuChange:(state,action)=>{
            if(action.payload.name!=='home'){
                state.currentMenu=action.payload
            }else{
                state.currentMenu={}
            }
        },
        HighlightKeyChange:(state,action)=>{
            state.HighlightKey=[]
            state.HighlightKey.push(action.payload)
        },
        // 面包屑
        selectCrumblist:(state,action)=>{
            if(action.payload.name!=='home'){
                const res=state.crumbList.findIndex((item)=>{return item.name===action.payload.name})
                if(res===-1){
                    state.crumbList.push(action.payload)
                }
            }
        },
        clearcrumb:(state,action)=>{
            let res=state.crumbList.findIndex((item)=>{return item.path===action.payload.pathname})
            state.crumbList.splice(res,1)
        },
        
    }
})
export const { collapseMenu,selectMenuList,closeTab,currentMenuChange,HighlightKeyChange,selectCrumblist,clearcrumb } = tabSlice.actions
export default tabSlice.reducer